package com.huangsm.cloud.cloudeurekaserver.controller;

import com.huangsm.cloud.cloudeurekaserver.entity.User;
import com.huangsm.cloud.cloudeurekaserver.mapper.UserMapper;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户接口
 * @author huangsm
 * @version V1.0
 */
@RestController
@Slf4j
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @Value("${server.port}")
    private String port;
    /**
     * 添加eureka客户端
     */
    @Autowired
    private EurekaClient eurekaClient;

    @GetMapping("/findUser/{id}")
    public User findUser(@PathVariable("id") Long id){
        log.info("调用了{}端口服务"+port);
        return userMapper.selectById(id);
    }

    /**
     * 得到注册服务的地址URL
     * @return
     */
    @GetMapping("/getHomeUrl")
    public String serviceUrl(){
        InstanceInfo instance = eurekaClient.getNextServerFromEureka("SERVICE-PROVIDER", false);
        return instance.getHomePageUrl();
    }
}
